<?php

namespace app\common\cachemodel\zl6;

use app\common\model\BaseCache;
use XznsTools\cache\CacheManage;

class Zl6MeditationUnlockData extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.zl6_meditation_unlock_data';

    protected $cacheModel = 'meditation.unlock_data';

    /**
     * 解锁信息
     * @var null | array
     */
    protected $unlockData = null;

    /**
     * 写入 解锁信息
     * @param int $uid
     * @param array $data
     * @return int
     */
    public function SaveData(int $uid, array $data)
    {
        try
        {
            $id = $this->insertGetId($data);

            CacheManage::getInstance()->setCacheConfig($this->cacheModel, $uid)->rmCache('list');
            return (int)$id;
        }
        catch (\Exception $e)
        {
            \think\Log::write(__CLASS__ . ' => ' . __FUNCTION__ . '()异常：' . $e->getMessage() . '失败信息：' . $e->getFile() . ':' . $e->getLine(), 'error', true);
            return 0;
        }
    }

    /**
     * 获取用户 解锁课程信息
     * @param int $uid
     * @return array
     * @throws \Exception
     */
    public function getUnlockIdsFromCache(int $uid)
    {
        return CacheManage::getInstance()->setCacheConfig($this->cacheModel, $uid)->ZsGetIocData('list', function () use ($uid)
        {
            return $this->getUnlockIdsFromDms($uid);
        });
    }

    /**
     * 数据库获取不同类型的推荐id
     * @param $uid
     * @return array
     */
    public function getUnlockIdsFromDms($uid)
    {
        try
        {
            $data = $this->where(['uid' => $uid])->order('id', 'DESC')->column('mid,sign', 'id');

            $resData = [0 => 0];
            foreach ($data as $id => $datum)
            {
                $resData[$datum['mid'] . ':' . $datum['sign']] = $id;
            }

            return $resData;
        }
        catch (\Exception $e)
        {
            return [];
        }
    }

    /**
     * 批量获取地图解锁结果信息
     * @param $ids
     * @param $type
     * @param $sortType
     * @return array|false|string|void
     */
    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        try
        {
            $data = $this->where(['id' => ['in', $ids]])->column('uid,mid,sign,order_num,create_time', 'id');
        }
        catch (\Exception $e)
        {
            $data = [];
        }
        return $data;
    }

    public function batchGetInfoByIds($cids, $field = [])
    {
        $resData = parent::batchGetInfoByIds($cids, $field);

        return $this->dataInit($resData);
    }

    /**
     * 数据初始化
     * @param array $data
     * @return array
     */
    public function dataInit(array $data)
    {
        foreach ($data as $index => $itemData)
        {
            $data[$index] = arrayKeyData2int($itemData, ['id', 'uid', 'mid', 'sign', 'create_time']);
        }

        return $data;
    }

    /**
     * 是否已解锁
     * @param int $uid
     * @param int $mid
     * @param int $sign
     * @return bool
     * @throws \Exception
     */
    public function isUnlock(int $uid, int $mid, int $sign = 0)
    {
        $unlockData = $this->getUnlockDatas($uid);

        return  in_array($mid . ':' . $sign,$unlockData,true);
    }

    /**
     * 已解锁信息
     * @param int $uid
     * @return array
     * @throws \Exception
     */
    public function getUnlockDatas(int $uid)
    {
        if ($this->unlockData === null)
        {
            $this->unlockData = $this->getUnlockIdsFromCache($uid);
        }

        return $this->unlockData;
    }
}